<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../layui/css/layui.css" tppabs="https://res.layui.com/layui/dist/css/layui.css"
          media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>

<!-- 修改 layui.js 文件路径-->
<script src="../layui/layui.js"></script>

<!-- 导入jquery.js-->
<script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.5.1.min.js"></script>


<body>

<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="deleteByIds">批量删除</button>
        <button class="layui-btn layui-btn-sm" lay-event="getExcel">导出Excel</button>
        <button class="layui-btn layui-btn-sm" lay-event="getExcelTemplate">验证是否全选</button>
        <button class="layui-btn layui-btn-sm" id="upload">导入Excel</button>
    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<!--<script src="//res.layui.com/layui/dist/layui.js" charset="utf-8"></script>-->
<!-- 注意：如果你直接复制所有代码到本地，上述 JS 路径需要改成你本地的 -->

<script>
    layui.use('table', function () {
        var table = layui.table;

        table.render({
            elem: '#test'
            , url: '/findLimitPage'/*tpa=https://www.layuiweb.com/test/table/demo1.json*/
            , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            , defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                , layEvent: 'LAYTABLE_TIPS'
                , icon: 'layui-icon-tips'
            }]
            , title: '患者数据表'
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'id', title: 'ID', width: 80, fixed: 'left', unresize: true, sort: true}
                , {field: 'country', title: '国家', width: 100, edit: 'text'}
                , {field: 'province', title: '省份', width: 100, edit: 'text'}
                , {field: 'patientName', title: '姓名', width: 100, edit: 'text'}
                , {field: 'patientType', title: '类型', width: 100, edit: 'text'}
                , {field: 'patientState', title: '状态', width: 100, edit: 'text'}
                , {field: 'createTime', title: '确诊时间', width: 150, edit: 'text'}
                , {field: 'cureTime', title: '治愈时间', width: 150, edit: 'text'}
                , {field: 'dieTime', title: '死亡时间', width: 150, edit: 'text'}

                , {fixed: 'right', title: '操作', toolbar: '#barDemo'}
            ]]
            , page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
                , curr: 1 //设定初始在第 1 页
                , limit: 10 //每页显示 3 条数据
                , groups: 3 //只显示 3 个连续页码
                , first: "首页" //显示首页
                , last: "尾页" //显示尾页
                , limits: [3, 5, 7] //选择每页显示条数
            }
        });

        //头工具栏事件
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'deleteByIds':
                    var data = checkStatus.data;
                    // layer.alert(JSON.stringify(data));

                    //获取所选对象的id，传参：patient/deleteByIds?&ids=1&ids=2&ids=3...
                    var ids = "";
                    //遍历所选的对象（实际上就是data）
                    for (let i = 0; i < data.length; i++) {
                        ids += "&ids=" + data[i].id
                    }
                    //alert("批量删除ids为：" + ids);
                    //判断
                    if(ids.length > 0){
                        $.get("/deleteByIds", ids,
                            function(data){
                                if("success" == data){
                                    //重新加载当前表格
                                    table.reload("test",{});
                                }
                            });
                    }

                    break;
                case 'getExcel':
                    var data = checkStatus.data;
                    // layer.alert(JSON.stringify(data));

                    //获取所选对象的id，传参：patient/deleteByIds?&ids=1&ids=2&ids=3...
                    var ids = "";
                    //遍历所选的对象（实际上就是data）
                    for (let i = 0; i < data.length; i++) {
                        ids += "&ids=" + data[i].id
                    }
                    location.href = "/getExcel?ids="+ids;
                    break;

                case 'getExcelTemplate':
                    //layer.msg(checkStatus.isAll ? '全选': '未全选');

                    //返回Excel模板
                    location.href = "/excel/patient.xlsx";

                    break;


                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            }
            ;
        });

        //监听行工具事件
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            //console.log(obj)
            if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    obj.del();
                    layer.close(index);
                    let deleteId = data.id;
                    $.get("/deleteById", {id: deleteId}, function (message) {
                        if (message == "success") {
                            alert("删除成功")
                            table.reload('test',{})
                        }

                    })

                });
            } else if (obj.event === 'edit') {
                // layer.prompt({
                //     formType: 2
                //     ,value: data.email
                // }, function(value, index){
                //     obj.update({
                //         email: value
                //     });
                //     layer.close(index);
                // });

                //alert(data);
                //alert(data.patientName);

                //data就是当前行对象
                queryOne(data);
            }
        });
    });
</script>


<script>
    function queryOne(data) {
        layer.open({
            type: 1,
            content: $("#updatePatientForm").html(),
            title: ['修改页面', 'font-size:18px;'],
            area: ['600px', '600px'],
            success: function () {
                $("#id").val(data.id);
                $("#country").val(data.country);
                $("#province").val(data.province);
                $("#patientName").val(data.patientName);

                if ("1" == data.patientType) {
                    $("#patientType").append("<option value='1' selected>" + '密接' + "</option> <option value='2'>" + '无症状' + "</option> <option value='3'>" + '时空伴随' + "</option>");
                } else if ("2" == data.patientType) {
                    $("#patientType").append("<option value='1' >" + '密接' + "</option> <option value='2' selected>" + '无症状' + "</option> <option value='3'>" + '时空伴随' + "</option>");
                } else if ("3" == data.patientType) {
                    $("#patientType").append("<option value='1' >" + '密接' + "</option> <option value='2'>" + '无症状' + "</option> <option value='3' selected>" + '时空伴随' + "</option>");
                }

                if ("1" === data.patientState) {
                    $("#patientState").append("<option value='1' selected>" + '重症' + "</option> <option value='2'>" + '治愈' + "</option>");
                } else if ("2" == data.patientState) {
                    $("#patientState").append("<option value='1' >" + '重症' + "</option> <option value='2' selected>" + '治愈' + "</option>");
                }

                $("#createTime").val(layui.util.toDateString(data.createTime, 'yyyy-MM-dd'));
                $("#cureTime").val(layui.util.toDateString(data.cureTime, 'yyyy-MM-dd'));
                $("#dieTime").val(layui.util.toDateString(data.dieTime, 'yyyy-MM-dd'));

                //渲染页面
                layui.form.render();
            },

            btn: ['提交', '取消']
            , yes: function (index, layero) {

                //修改操作
                $.get("/update", $("#updatePatient").serialize(),
                    function (data) {
                        //alert(data)
                        if ("success" == data) {
                            location.reload();//刷新当前页面
                        }
                    });

            }
            , btn2: function (index, layero) {
                alert("取消")
            }

        });
    }
</script>

<script type="text/html" id="updatePatientForm">

    <form class="layui-form" id="updatePatient">

        <!-- 把id 作为隐藏域传过去 -->
        <input type="hidden" name="id" id="id">

        <div class="layui-form-item">
            <label class="layui-form-label">国家</label>
            <div class="layui-input-inline">
                <input type="text" name="country" value="中国" readonly="readonly" id="country" required
                       lay-verify="required" placeholder="请输入国家" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">省份</label>
            <div class="layui-input-inline">
                <input type="text" name="province" id="province" required
                       lay-verify="required" placeholder="请输入省份" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">姓名</label>
            <div class="layui-input-inline">
                <input type="text" name="patientName" id="patientName" required
                       lay-verify="required" placeholder="请输入姓名" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">类型</label>
            <div class="layui-input-inline">
                <select name="patientType" id="patientType" lay-verify="required">

                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-inline">
                <select name="patientState" id="patientState" lay-verify="required">

                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">确诊时间</label>
                <div class="layui-input-inline">
                    <input type="date" name="createTime" id="createTime"
                           lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">治愈时间</label>
                <div class="layui-input-inline">
                    <input type="date" name="cureTime" id="cureTime"
                           lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">死亡时间</label>
                <div class="layui-input-inline">
                    <input type="date" name="dieTime" id="dieTime"
                           lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>

    </form>

</script>

</body>

<!-- 上传 -->
<script>
    layui.use('upload', function(){
        var upload = layui.upload;

        //执行实例
        var uploadInst = upload.render({
            elem: '#upload' //绑定元素
            ,accept: 'file' //可以上传任意类型的文件
            ,url: '/upload' //上传接口
            ,done: function(res){
                //上传完毕回调
                if("success" == res.msg){
                    alert("导入数据成功！")
                    location.reload();//刷新当前页面
                }
            }
            ,error: function(){
                //请求异常回调
            }
        });
    });
</script>
</html>